package com.cn.kabalifemerchants.ui.login;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.utils.TimeUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/23.
 * function:
 */
public class ForgetPwdActivity extends IBaseActivity<ForgetPwdView, ForgetPwdPresenter> implements ForgetPwdView {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_btn_code)
    TextView tvBtnCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    MyCountDownTimer myCountDownTimer;
    @BindView(R.id.img_btn_pwd)
    ImageView imgBtnPwd;
    @BindView(R.id.tv_title_top)
    TextView tv_title_top;

    int pwdSign = 0;

    Bundle bundle;
    int sign;

    @Override
    protected ForgetPwdPresenter createPresenter() {
        return new ForgetPwdPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_pwd;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
            myCountDownTimer = null;
        }
    }

    @Override
    public void leftFinish() {
        super.leftFinish();
        hideKeyboard(etCode);
        finish();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
//        bundle = getIntent().getExtras();
//        sign = bundle.getInt("sign");
//        if (sign == 1) {
//            tv_title_top.setText("忘记密码");
//        } else tv_title_top.setText("修改密码");
        setTitleBar("修改密码");
        myCountDownTimer = new MyCountDownTimer(60000, 1000);
    }

    @OnClick({R.id.tv_btn_code, R.id.tv_btn_next, R.id.img_btn_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_btn_pwd:
//                if (pwdSign == 0) {
//                    imgBtnPwd.setImageResource(R.drawable.p_true);
//                    pwdSign = 1;
//                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                } else {
//                    imgBtnPwd.setImageResource(R.drawable.p_false);
//                    pwdSign = 0;
//                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                }
                break;
            case R.id.tv_btn_code:
                if (!TextUtils.isEmpty(etPhone.getText().toString().trim())) {
                    mPresenter.onPhoneCodeData(etPhone.getText().toString().trim(), 2);
                } else RxToast.error("请输入手机号");

                break;
            case R.id.tv_btn_next:
                if (!TextUtils.isEmpty(etPhone.getText().toString().trim())) {
                    if (!TextUtils.isEmpty(etCode.getText().toString().trim())) {
                        if (!TextUtils.isEmpty(etPwd.getText().toString().trim())) {
                            String time1 = TimeUtils.getTime10();
                            mPresenter.onPwdUpData(etPhone.getText().toString().trim(),
                                    etPwd.getText().toString().trim(),
                                    etCode.getText().toString().trim());
                        } else RxToast.error(getResources().getString(R.string.pwd_null_hi));
                    } else RxToast.error(getResources().getString(R.string.code_null_hi));
                } else RxToast.error(getResources().getString(R.string.phone_null_hi));
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onPhoneCodeSuccess(MsgBean bean) {
        myCountDownTimer.start();
        RxToast.success(bean.getMsg());
    }

    @Override
    public void onPwdUpSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        hideKeyboard(etCode);
        finish();
    }


    /**
     * 倒计时函数
     */
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            tvBtnCode.setClickable(false);
            tvBtnCode.setText(l / 1000 + getResources().getString(R.string.time_second_str));

        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //设置可点击
            tvBtnCode.setText(getResources().getString(R.string.time_again_str));
            tvBtnCode.setClickable(true);
        }
    }
}
